package com.mo.tools.serialize;

import java.io.*;

/**
 * @name: SerializationUtils
 * @description: 序列化工具
 * @author: Yang Xingfu
 * @date: 2020-01-06 18:53
 **/
public class SerializationUtils {

    /**
     * 序列化二进制对象数组
     * @param obj Object对象
     * @return 二进制数组
     */
    public static byte[] serialize(Object obj) {
        ObjectOutputStream objectOutputStream = null;

        if (obj == null) {
            return null;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
            objectOutputStream.flush();
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to serialize object of type: " + obj.getClass());
        }  finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return byteArrayOutputStream.toByteArray();
    }


    /**
     * 序列化对象到文件
     * @param obj 序列化对象
     * @param outputStream 输出流
     */
    public static void serialize(Object obj, OutputStream outputStream) {
        ObjectOutputStream objectOutputStream = null;

        if (outputStream == null) {
            throw new IllegalArgumentException("The OutputStream object must bu null");
        }

        try {
            objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(obj);
            objectOutputStream.flush();
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to serialize object of type: " + obj.getClass());
        } finally {
            if (outputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 反序列化对象
     * @param bytes 二进制数组
     * @return 对象
     */
    public static Object deserialize(byte[] bytes) {
        ObjectInputStream objectInputStream = null;

        if (bytes == null) {
            return null;
        }

        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to deserialize Object of type: " + bytes.getClass());
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("The ByteArrayInputStream file path error");
        } finally {
            if(objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Object deserialize(InputStream inputStream) {
        ObjectInputStream objectInputStream = null;

        if (inputStream == null) {
            throw new IllegalArgumentException("The InputStream is not by null");
        }
        try {
            objectInputStream = new ObjectInputStream(inputStream);
            return objectInputStream.readObject();

        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to deserialize Object of");
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("The InputStream file path error");
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
